import EntityTypes from "../Constants/EntityTypes";

/**
 * 资源加载类
 * created by Five on 2018-11-5 17:44:45
 */
export default class ResHelper {
    private poolArr: Map<string, cc.NodePool>;

    constructor() {
        this.poolArr = new Map<string, cc.NodePool>();
    }

    getPool(name: string): cc.NodePool {
        let pool = this.poolArr.get(name);
        if (pool == null) {
            pool = new cc.NodePool(name);
            this.poolArr.set(name, pool);
        }
        return pool
    }

    public Put(node: cc.Node){
        this.getPool(node.name).put(node);
    }

    public async GetRes(name: string) {
        let pool = this.getPool(name);
        if (pool.size() > 0)
            return pool.get();

        return new Promise((resolve, reject) => {
            cc.loader.loadRes('prefabs/' + name.trim(), cc.Prefab, (error, res) => {
                if (error) {
                    cc.log("get resource Failed" + error);
                    reject();
                } else {
                    resolve(cc.instantiate(res));
                }
            })
        });
    }

    public async GetClip(name: string) {
        return new Promise((resolve, reject) => {
            cc.loader.loadRes('clips/' + name.trim(), cc.AnimationClip, (error, res) => {
                if (error) {
                    cc.log("get clip Failed" + error);
                    reject();
                } else {
                    resolve(res);
                }
            })
        });
    }

    public async GetResScript(name: string) {
        let pool = this.getPool(name);
        if (pool.size() > 0)
            return pool.get().getComponent(name);

        return new Promise((resolve, reject) => {
            cc.loader.loadRes('prefabs/' + name.trim(), cc.Prefab, (error, res) => {
                if (error) {
                    cc.log("get resource script Failed" + error);
                    reject();
                } else {
                    resolve(cc.instantiate(res).getComponent(name));
                }
            })
        });
    }

    public async GetSpriteFrame(name: string) {
        return new Promise((resolve, reject) => {
            cc.loader.loadRes('texuture/' + name.trim(), cc.SpriteFrame, function (error: Error, spriteFrame) {
                if (error) {
                    console.error('GetSpriteFrame fail: error=', error);
                    reject();
                } else {
                    resolve(spriteFrame);
                }
            });
        });
    }

    public async GetIcon(name: string) {
        return new Promise((resolve, reject) => {
            cc.loader.loadRes('icons/' + name.trim(), cc.SpriteFrame, function (error: Error, spriteFrame) {
                if (error) {
                    console.error('GetIcon fail: error=', error);
                    reject();
                } else {
                    resolve(spriteFrame);
                }
            });
        });
    }

    public async LoadFile(dir: string, fileName: string) {
        return new Promise((resolve, reject) => {
            cc.loader.loadRes(dir.trim() + "/" + fileName.trim(), function (err, assets) {
                if (err) {
                    console.error(err);
                    reject();
                } else {
                    resolve(assets)
                }
            })
        })
    }

    public async LoadDir(dir: string) {
        return new Promise((resolve, reject) => {
            cc.loader.loadResDir(dir.trim(), function (err, assets) {
                if (err) {
                    console.error(err);
                    reject();
                } else {
                    resolve(assets)
                }
            })
        })
    }
}